/**
 *
 * 提供素材库的弹出层，如：图文、图片、语音、视频
 */
define(function (require, exports, module) {
    var angular = require('angular');

    var util = require("util");

    require('./picture_service');


    require('business_modules/material/modules/voice/voiceService');

    var service = angular.module('services.dialog',['services.picture','material.voiceService','material.videoService']);
    service.service('imgDialogService',['pictureService',function(pictureService){
        this.showImages = function($dialogScope){
            $dialogScope.pictures = pictureService;
            $dialogScope.selPictureId = '';
            $dialogScope.changeSelPictureId = function(id){
                $dialogScope.selPictureId = id;
            };
            //加载图片库
            $dialogScope.pictures.getGroup();
        };

        this.okImages = function($dialogScope){
            var picture = $dialogScope.pictures.getPictureById($dialogScope.selPictureId);
            if(picture && $dialogScope.parent.addImage){
                $dialogScope.parent.addImage(picture);
            }else{
                util.notify('缺少回调函数addImage');
            }
        }
    }]);
    service.service('voiceDialogService',['$rootScope','voiceService','limit',function($rootScope,voiceService,limit){
        this.showVoices = function($dialogScope){
            var $scope = $dialogScope;
            $scope.pagination = {
                pages: 0,
                current: 1,
                change: function (page) {
                    $scope.searchVoiceList(page);
                }
            };
            $scope.searchVoiceList = function (page) {
                voiceService.getVoiceList({
                    search: {
                        limit: limit,
                        offset: limit * (page - 1)
                    },
                    success: function (res) {
                        $scope.voiceList = res.data.data;
                        $scope.pagination.pages = Math.ceil(res.data.total / limit);
                    },
                    fail: function (res) {
                        $rootScope.$emit('httpRequestFail','获取语音列表失败');
                    }
                });
            };
            $scope.selId = '';
            $scope.changeSelId = function(id,voice){
                $scope.selId = id;
                $scope.selVoice = voice;
            };
            $scope.searchVoiceList(1);
        };

        this.okVoices = function($dialogScope){

            var selVoice = $dialogScope.selVoice;
            if(selVoice && $dialogScope.parent.addVoice){
                $dialogScope.parent.addVoice(selVoice);
            }else{
                util.notify('缺少回调函数addVoice');
            }
        }
    }]);
    service.service('videoDialogService',['$rootScope','videoService','limit',function($rootScope,videoService,limit){
        this.showVideos = function($dialogScope){
            var $scope = $dialogScope;
            $scope.pagination = {
                pages : 0,
                current : 1,
                change: function (page) {
                    $scope.searchVideoList(page);
                }
            };

            $scope.searchVideoList = function (page) {
                videoService.getVideoList({
                    search:{
                        limit: limit,
                        offset: limit * (page - 1)
                    },
                    success: function (res) {
                        $scope.videoList = res.data.data;
                        $scope.videoCount = res.data.total;
                        $scope.pagination.pages = Math.ceil(res.data.total / limit);
                    },
                    fail: function (res) {
                        $rootScope.$emit('httpRequestFail','获取视频列表失败');
                    }
                });
            };
            $scope.selId = '';
            $scope.changeSelId = function(id,video){
                $scope.selId = id;
                $scope.selVideo = video;
            };
            $scope.searchVideoList(1);
        };

        this.okVideos = function($dialogScope){

            var selVideo = $dialogScope.selVideo;
            if(selVideo && $dialogScope.parent.addVideo){
                $dialogScope.parent.addVideo(selVideo);
            }else{
                util.notify('缺少回调函数addVideo');
            }
        }
    }]);
})